
var submitButton=document.getElementById('submit');
var messageElement=document.getElementById('message');
var myFormElement=document.getElementById('myForm');
submitButton.onclick=function(e) {
        e.preventDefault();
        var elementUsername = document.querySelector('#myForm input[name=username]');
        var username = elementUsername.value;
        if (username == "") {
            messageElement.innerHTML = '账号不能为空';
            return;
        };
        var passwordElement = document.querySelector('#myForm input[name=password]');
        var password = passwordElement.value;
        if (password == "") {
            messageElement.innerHTML = '密码不能为空';
            return;
        };
        var repasswordElement = document.querySelector('#myForm input[name=repassword]');
        var repassword = repasswordElement.value;
        if (repassword == "") {
            messageElement.innerHTML = '重复密码不能为空';
            return;
        }
        if(repassword !=password ){
            messageElement.innerHTML = '密码不一致！';
        };
        var nichengElement = document.querySelector('#myForm input[name=nicheng]');
        var nicheng = nichengElement.value;
        if (nicheng == "") {
            messageElement.innerHTML = '昵称不能为空';
            return;
        };
        var csriElement = document.querySelector('#myForm input[name=csri]');
        var csri = csriElement.value;
        if (csri == "") {
            messageElement.innerHTML = '出生日期不能为空';
            return;
        };
        var sexElement = document.querySelector('#myForm input[name=sex]:checked');
        var sex = sexElement.value;
    console.log(sex);
        if (sex == "") {
            messageElement.innerHTML = '性别不能为空';
            return;
        };
        var userImgElement = document.querySelector('#myForm input[name=userImg]');
        var userImg = userImgElement.value;
        if (userImg == "") {
        messageElement.innerHTML = '头像不能为空';
        return;
        };
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    var result = JSON.parse(this.responseText);
                    if (result.error == 1) {
                        messageElement.innerHTML = result.message;
                    } else {
                        location.href = '../login.html';
                    }
                } else {
                    alert("异常");
                }
            }
        };
        xhr.open(myFormElement.method, myFormElement.action);
        var data = new FormData();
        data.append('username', username);
        data.append('password', password);
        data.append('repassword', repassword);
        data.append('nicheng', nicheng);
        data.append('csri', csri);
        data.append('sex', sex);
        data.append('userImg', userImgElement.files[0]);
        xhr.send(data);
    }
